package net.phoenix.repository.value;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;

import javax.jcr.Binary;
import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;

import net.phoenix.repository.InputStreamBinary;
import net.phoenix.repository.RepositoryRuntimeException;

/**
 * 
 * Jigsaw server
 * 
 * @author <a href="shamphone@gmail.com">Li XiongFeng</a>
 * @date 2012-1-3
 * @version 1.0.0
 */
public class StringValue extends BasicValue {
	private String value;

	public StringValue(String value) {
		this.value = value;
	}

	public InputStream getStream() {
		if (this.value == null)
			return null;
		return new ByteArrayInputStream(value.getBytes());

	}

	public void setValue(InputStream is) throws RepositoryException {
		if (is == null) {
			this.value = null;
			return;
		}
		byte[] cache = new byte[1024];
		int cursor = 0;
		int length = 1024;
		StringBuffer buffer = new StringBuffer();
		try {
			if (is.available() < length)
				length = is.available();
			while (is.read(cache, cursor, length) > 0) {
				buffer.append(new String(cache, "UTF-8"));
				cursor += 1024;
				if (is.available() < length)
					length = is.available();
			}
			this.value = buffer.toString();
		} catch (UnsupportedEncodingException ex) {
			throw new RepositoryException(ex);
		} catch (IOException ex) {
			throw new RepositoryException(ex);
		}

	}

	/**
	 * getType
	 * 
	 * @return int
	 */
	public int getType() {
		return PropertyType.STRING;
	}

	public String getString() {
		return this.value;
	}

	public long getLong() throws ValueFormatException {
		if ((this.value == null) || (this.value.length() == 0))
			return 0l;
		else
			try {
				return Long.parseLong(this.value);
			} catch (NumberFormatException ex) {
				throw new ValueFormatException(ex);
			}
	}

	public double getDouble() throws ValueFormatException {
		if ((this.value == null) || (this.value.length() == 0))
			return 0l;
		else
			try {
				return Double.parseDouble(this.value);
			} catch (NumberFormatException ex) {
				throw new ValueFormatException(ex);
			}

	}

	public Calendar getDate() {
		Date date = DateValue.toDate(this.value);
		if (date == null)
			return null;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar;
	}

	public boolean getBoolean() throws ValueFormatException {
		if ((this.value == null) || (this.value.length() == 0))
			return false;
		try {
			return Boolean.parseBoolean(this.value);
		} catch (NumberFormatException ex) {
			throw new ValueFormatException(ex);
		}

	}

	public int compareTo(Value o) {
		Value value = (Value) o;
		try {
			return this.value.compareTo(value.getString());
		} catch (RepositoryException e) {
			throw new RepositoryRuntimeException(e);
		}
	}

	public boolean equals(Object o) {
		if (o == null)
			return false;
		if (o instanceof Value)
			try {
				return ((Value) o).getString().equals(this.value);
			} catch (RepositoryException e) {
				throw new RepositoryRuntimeException(e);
			}
		return o.toString().equals(this.value);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Value#getBinary()
	 */
	@Override
	public Binary getBinary() throws RepositoryException {
		if (this.value != null)
			return new InputStreamBinary(this.value);
		return null;
	}
}
